package com.stress.testing.plugin.netflix.zuul.interceptor;

import com.netflix.zuul.context.RequestContext;
import com.stress.testing.common.StressTesting;
import com.stress.testing.common.TraceContext;
import com.stress.testing.common.TraceContextHolder;
import com.stress.testing.common.interceptor.AbstractInterceptor;

import java.lang.reflect.Method;

public class ZuulServletPreRouteInterceptor extends AbstractInterceptor {
    @Override
    public void before(Method method, Object[] args, Object zuper, Object thiz) {
        if (!StressTesting.enable(getAgentConfig(), getPluginConfig())) {
            return;
        }

        TraceContext traceContext = TraceContextHolder.getTraceContext();
        if (traceContext == null) {
            return;
        }

        RequestContext currentContext = RequestContext.getCurrentContext();
        if (currentContext == null) {
            return;
        }

        // 添加链路信息
        for (String name : traceContext.propertyNames()) {
            currentContext.addZuulRequestHeader(name, traceContext.getProperty(name));
        }
    }
}
